﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class manage_Default2 : System.Web.UI.Page
{
    protected clsAdvertisement ads;
    protected Functions func;
    int id = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            
            try
            {
                id = Convert.ToInt32(Request.QueryString["id"].ToString());
            }
            catch { }
            if (id > 0)
            {
                ads = new clsAdvertisement();
                ads = ads.GetAdv(id);
                ads.Id = id;
                txtUrl.Text = ads.Url;
                txtName.Text = ads.Name;
                txtInfo.Text = ads.Information;
                txtPhone.Text = ads.Phone_number;
                txtAddress.Text = ads.Address;
                string path = "~/images/adv/";
                ImageLoGo.ImageUrl = path + ads.Image_url;
                                
            }
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        ads = new clsAdvertisement();
        try
        {
            id = Convert.ToInt32(Request.QueryString["id"].ToString());
        }
        catch { }
        ads.Id = id;
        if (fImage == null || fImage.FileName.Equals(ads.Image_url))
        {
            
            ads.Url = txtUrl.Text;
            ads.Image_url = "images/adv/" + fImage.FileName;
            ads.Name = txtName.Text;
            ads.Information = txtInfo.Text;
            ads.Phone_number = txtPhone.Text;
            ads.Address = txtAddress.Text;
            if (ads.updateAds() > 0)
            {
                Response.Redirect("Default.aspx");
            }

        }
        func = new Functions();
        string strFileSavePath = Server.MapPath("../images/adv/");
        int fileUpload = func.checkFileUpload(fImage, strFileSavePath);
        if (fileUpload == 1)
        {
            ads.Url = txtUrl.Text;
            ads.Image_url = "images/adv/" + fImage.FileName;
            ads.Name = txtName.Text;
            ads.Information = txtInfo.Text;
            ads.Phone_number = txtPhone.Text;
            ads.Address = txtAddress.Text;
            if (ads.updateAds() > 0)
            {
                Response.Redirect("Default.aspx");
            }
        }
        else
        {
            if (fileUpload == 2)
            {
                //lbError.Text = "File upload không đúng định dạng file hình ảnh. ";
                lbError.Text = "";
            }
            else if (fileUpload == 3)
            {
                lbError.Text = "Lỗi khi upload file. ";
            }
        }
    }
}
